/*
 * Created by:		Mason Staerkel
 * 					Jacob Julich
 * Created on:		March 23, 2011
 * Last updated:	March 23, 2011
 * Function:		Will act as the canvas for our objectdraw program
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Canvas2 extends JFrame
{
	private int startX, startY;
	private int endX, endY;
	private String currentTool;
	JLabel instructions = new JLabel("Drag the mouse to draw");
	private int tally;
	int marker;	// holds the value of the current selected object
	Graphics [] holdGraphic;
	Graphics currentGraphic;
	int [][] positions;
	Dimension tools = new Dimension(75, 75);
	JPanel tarp = new JPanel();
	
	public Canvas2()
	{
		// Creates the JFrame
		super( "A simple paint program" );
		
		// Initializes variables
		tally = 0;
		marker = -5;
		currentTool = "rect";
		holdGraphic = new Graphics[100];
		positions = new int[100][4];
		

		tarp.setBackground(Color.WHITE);
		add(tarp, BorderLayout.CENTER);
		
		//JLabel instructions = new JLabel("Drag the mouse to draw");
		add( instructions, BorderLayout.SOUTH);
		
		//Adds a toolbar
		ToolBar tb = new ToolBar();
		add(tb, BorderLayout.WEST);
		
		//Adds a menu
		/*
		JMenu toolMenu = new JMenu();
		toolMenu.setText("Tool");
		add(toolMenu, BorderLayout.NORTH);
		*/
		
		MouseHandler handler = new MouseHandler();
		tarp.addMouseListener(handler);
	}	// end constructor
	
	private void paint(Graphics g, int counter)
	{
		int changeX, changeY;
		changeX = endX - startX;
		changeY = endY - startY;
		
		// Below checks if the rectangle was drawn in reverse or not and compensates accordingly
		if ((changeX < 0) && (changeY < 0))
		{
			g.drawRect(endX, endY, -changeX, -changeY);
			positions[counter][0] = endX;
			positions[counter][1] = endY;
			positions[counter][2] = -changeX;
			positions[counter][3] = -changeY;
		}
		else if (changeX < 0)
		{
			g.drawRect(endX, startY, -changeX, changeY);
			positions[counter][0] = endX;
			positions[counter][1] = startY;
			positions[counter][2] = -changeX;
			positions[counter][3] = changeY;
		}
		else if (changeY < 0)
		{
			g.drawRect(startX, endY, changeX, -changeY);
			positions[counter][0] = startX;
			positions[counter][1] = endY;
			positions[counter][2] = changeX;
			positions[counter][3] = -changeY;
		}
		else
		{
			g.drawRect(startX, startY, changeX, changeY);
			positions[counter][0] = startX;
			positions[counter][1] = startY;
			positions[counter][2] = changeX;
			positions[counter][3] = changeY;
		}
		
		this.holdGraphic[counter] = g;
	}
	
	private int checkForObject()
	{
		int hold = -5;
		for(int count = 0; count < holdGraphic.length; count++)
		{
			if((endX > positions[count][0]) && (endX < positions[count][2]))
				if((endY > positions[count][1]) && (endY < positions[count][3]))
					hold = count;
				
		}
		return hold;
	}
	
	private class ToolBar extends JPanel
	{

		public ToolBar()
		{
			setLayout(new GridLayout(10, 1, 5, 15));
			
			JButton rectangle = new JButton("Rect");
			JButton objSelector = new JButton("Obj");
			
			rectangle.setPreferredSize(tools);
			objSelector.setPreferredSize(tools);
			
			rectangle.addActionListener(
					new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							currentTool = "rect";
						}
					}
			);
			
			objSelector.addActionListener(
					new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							currentTool = "obj";
						}
					}
			);
			
			add(rectangle);
			add(objSelector);
		}
	}

	private class MouseHandler implements MouseListener
	{
		// MouseListener event handlers
		// event when mouse is pressed
		@Override
		public void mousePressed( MouseEvent e)
		{
			startX = e.getX() + 82;
			startY = e.getY() + 30;
		}
		
		// event when mouse is released
		@Override
		public void mouseReleased( MouseEvent e)
		{
			endX = e.getX() + 82;
			endY = e.getY() + 30;
			
			Graphics g = getGraphics();
			
			if(currentTool == "rect")
				paint(g, tally++);
			else if(currentTool == "obj")
			{
				marker = checkForObject();
				
				if (marker != -5)
				{
					holdGraphic[marker].fillRect(positions[marker][0], positions[marker][1], positions[marker][2], positions[marker][3]);
				}
				String newText = String.format("marker is: %d", marker);
				//String pos = String.format("posX1 = %d posX2 = %d posY1 = %d posY2 =%d, currX = %d currY = %d", positions[marker][0], positions[marker][2], positions[marker][1], positions[marker][3], e.getX(), e.getY());
				instructions.setText(newText);
			}
		}

		// event when mouse is released immediately after a press
		@Override
		public void mouseClicked(MouseEvent e)
		{
			
		}

		// event when mouse enters area
		@Override
		public void mouseEntered(MouseEvent e)
		{

		}

		// event when mouse exits area
		@Override
		public void mouseExited(MouseEvent e)
		{
			
		}
	}	// end inner class MouseHandler
}	// end class Canvas